<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${basePackage}.${interface}.${domainNameUpperCamel}${interfaceType}">

 <!-- Defining a data set -->
   <resultMap id="BaseResultMap" type="${basePackage}.entity.${domainNameUpperCamel}${entity}">
    <#list dataList as var>
     <#if var_index == 0>
      <id column="${var.columnName}" property="${var.newColumn}" />
     <#else>
      <result column="${var.columnName}" property="${var.newColumn}" />
     </#if>
    </#list>
   </resultMap>

   <sql id="Base_Column_List">
      <#list dataList as var>
       u.${var.columnName}<#if var_has_next>,</#if>
      </#list>
   </sql>

   <!-- Insert data -->
   <insert id="insert" parameterType="${basePackage}.entity.${domainNameUpperCamel}${entity}">
       <#if autoIncrement?exists>
      <selectKey resultType="java.lang.${resutId}" keyProperty="id" order="AFTER" >
        SELECT LAST_INSERT_ID()
      </selectKey>
       </#if>
      INSERT INTO
       ${tableName}(
       <#list dataList as var>
        ${var.columnName}<#if var_has_next>,</#if>
       </#list>
       )VALUES(
       <#list dataList as var>
        ${r"#{"}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
       </#list>
       )
   </insert>
   <!-- Insert non-empty data -->
   <insert id="insertSelective" parameterType="${basePackage}.entity.${domainNameUpperCamel}${entity}">
   <#if autoIncrement?exists>
     <selectKey resultType="java.lang.${resutId}" keyProperty="id" order="AFTER" >
        SELECT LAST_INSERT_ID()
     </selectKey>
   </#if>
    INSERT INTO ${tableName}
      <trim prefix="(" suffix=")" suffixOverrides="," >
       <#list dataList as var>
        <if test='${var.newColumn} != null'>
         ${var.columnName}<#if var_has_next>,</#if>
        </if>
       </#list>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides="," >
       <#list dataList as var>
        <if test='${var.newColumn} != null'>
         ${r"#{"}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
        </if>
       </#list>
      </trim>
   </insert>

   <!-- Batch Inserting Data -->
   <insert id="insertBatch"  parameterType="list">
     INSERT INTO
     ${tableName}(
      <#list dataList as var>
       ${var.columnName}<#if var_has_next>,</#if>
      </#list>
      )VALUES
      <foreach collection="list"  item="item"  separator="," >
       (
       <#list dataList as var>
        ${r"#{item."}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
       </#list>
       )
      </foreach>
   </insert>
   <!-- Update data by ID -->
   <update id="update" parameterType="${basePackage}.entity.${domainNameUpperCamel}${entity}">
      UPDATE ${tableName}
      SET
      <#list dataList as var>
       ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
      </#list>
      WHERE
      <#list dataList as var>
       <#if var_index == 0>
        ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
       </#if>
      </#list>
   </update>
   <!-- Update non-empty data by ID -->
   <update id="updateSelective" parameterType="${basePackage}.entity.${domainNameUpperCamel}${entity}">
      UPDATE ${tableName}
      <set>
       <#list dataList as var>
        <if test='${var.newColumn} != null'>
         ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
        </if>
       </#list>
      </set>
      WHERE
      <#list dataList as var>
       <#if var_index == 0>
        ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
       </#if>
      </#list>
   </update>
   <!-- Physical deletion is performed by ID -->
   <delete id="deleteById">
      DELETE FROM ${tableName}
      WHERE
      <#list dataList as var>
       <#if var_index == 0>
        ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
       </#if>
      </#list>
   </delete>

   <!-- Batch physical deletion is performed by ID -->
   <delete id="deleteBatchById">
      DELETE FROM ${tableName}
      WHERE
      <#list dataList as var>
       <#if var_index == 0>
        ${var.columnName} IN
       </#if>
      </#list>
      <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
       ${r"#{"}item${r"}"}
      </foreach>
   </delete>
   <!-- Load data by ID -->
   <select id="findById" parameterType="${pkDataType}" resultMap="BaseResultMap">
      SELECT <include refid="Base_Column_List" />
      FROM ${tableName} u
      WHERE
      <#list dataList as var>
       <#if var_index == 0>
        u.${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
       </#if>
      </#list>
   </select>

   <!-- Get data list -->
   <select id="queryListAll" parameterType="map" resultMap="BaseResultMap">
      SELECT <include refid="Base_Column_List" />
      FROM ${tableName} u
      <where>
       <foreach collection="parameter.entrySet()" index="column" item="value">
        <if test="column != null and column != ''">
         and ${r'u.${column}'}  = ${r'#{value}'}
        </if>
       </foreach>
      </where>
   </select>

</mapper>
